package com.tsing.cli.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.tsing.cli.common.annotation.Logger;
import com.tsing.cli.common.exception.CustomException;
import com.tsing.cli.common.exception.ResponseCode;
import com.tsing.cli.common.vo.PageResultVO;
import com.tsing.cli.common.vo.ResultData;
import com.tsing.cli.system.service.ILogService;
import com.tsing.cli.system.vo.req.LogPageReqVO;
import com.tsing.cli.system.vo.resp.LogPageRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 操作日志表 前端控制器
 * </p>
 *
 * @author TheTsing
 * @since 2022-11-25 09:47
 */
@Tag(name = "操作日志")
@RestController
@RequestMapping("/log")
@RequiredArgsConstructor
public class LogController {

    private final ILogService logService;

    @DeleteMapping
    @Operation(summary = "删除日志")
    @Logger(description = "删除日志")
    @PreAuthorize("@ex.check('log:delete')")
    public ResultData deleteLog(@RequestBody List<String> reqVO) {
        if (CollUtil.isEmpty(reqVO)) {
            throw new CustomException(ResponseCode.METHOD_ARGUMENT_NOT_VALID_EXCEPTION);
        }
        logService.deleteLog(reqVO);
        return ResultData.success();
    }

    @GetMapping
    @Operation(summary = "日志分页")
    @PreAuthorize("@ex.check('log:list')")
    public ResultData<PageResultVO<LogPageRespVO>> logPage(@Validated @ParameterObject LogPageReqVO reqVO) {
        return ResultData.success(logService.logPage(reqVO));
    }

}
